package com.foton.logistic.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.foton.logistic.R;
import com.foton.logistic.adapter.RemindAdapter;
import com.foton.logistic.base.BaseActivity;
import com.foton.logistic.listener.IOnRefreshListener;
import com.foton.logistic.listener.IOnResultListener;
import com.foton.logistic.listener.IOnTryClickListener;
import com.foton.logistic.model.RemindResult;
import com.foton.logistic.task.DispatchTask;
import com.foton.logistic.task.ShowLoadTask;
import com.foton.logistic.task.TaskConstant;
import com.foton.logistic.util.BaseConstant;
import com.foton.logistic.util.net.HttpUtil;
import com.foton.logistic.view.ultimate.UltimateRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * Created by wuhaocong on 2017/7/26.
 */

public class RemindActivity extends BaseActivity {
    @InjectView(R.id.ft_fragment_remind_list_rv)
    UltimateRecyclerView ultimateRecyclerView;
    @InjectView(R.id.base_view_contain_layout)
    LinearLayout baseViewContainLayout;
    private LinearLayoutManager linearLayoutManager;
    private RemindAdapter adapter;
    private int pageNumber;
    private RemindResult res;
    private List<RemindResult.DataEntity> resultList = new ArrayList<>();
    public List<RemindResult.DataEntity> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ft_ui_remind);
        ButterKnife.inject(this);
    }

    @Override
    public void init() {
        setBackLayoutVisibility(View.VISIBLE);
        setTitleText("提醒");
        setTitleTextVisibility(View.VISIBLE);
        initUltimate();
        refresh(true);
    }

    private void initUltimate() {
        linearLayoutManager = new LinearLayoutManager(this);
        ultimateRecyclerView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        adapter = new RemindAdapter(this, list);
        ultimateRecyclerView.setAdapter(adapter);
        ultimateRecyclerView.enableLoadmore(false);
        ultimateRecyclerView.enableSwipeRefresh(false);
        ultimateRecyclerView.endFinish(false);
        ultimateRecyclerView.addItemDividerDecoration(this);
        ultimateRecyclerView.setOnRefreshListener(new IOnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh(false);
            }

            @Override
            public void onLoadMore() {
                getRecordList(false);
            }
        });
    }

    void refresh(boolean showLoad) {
        pageNumber = BaseConstant.PAGER_START;
        getRecordList(showLoad);
    }

    IOnTryClickListener iOnTryClickListener = new IOnTryClickListener() {
        @Override
        public void onTry() {
            refresh(true);
        }

        @Override
        public void onOption() {

        }
    };

    void getRecordList(final boolean showLoad) {
        String url = BaseConstant.getRemindMessage;
        Map<String, Object> argMap = HttpUtil.getEncryMap(this);
        argMap.put("pageNumber", "" + pageNumber);
        argMap.put("pageSize", "" + BaseConstant.PAGER_COUNT);
        ShowLoadTask task = new ShowLoadTask(this, taskTag, baseViewContainLayout, loadLayout, "", showLoad, iOnTryClickListener, url, argMap, TaskConstant.POST);
        task.setParseClass(RemindResult.class);
        task.setiOnResultListener(new IOnResultListener() {
            @Override
            public void onOK(DispatchTask task) {
                if (task.resultEntity instanceof RemindResult) {
                    res = (RemindResult) task.resultEntity;
                    resultList = res.data;
                }
                updateInfo((ShowLoadTask) task);
            }


            @Override
            public void onError(DispatchTask task) {

            }

            @Override
            public void onDone(DispatchTask task) {
                try {
                    ultimateRecyclerView.refreshFinish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        task.execute();
    }

    void updateInfo(ShowLoadTask task) {


        try {

            if (resultList != null) {
                if (pageNumber == BaseConstant.PAGER_START) {
                    list.clear();
                }
                pageNumber++;
                for (RemindResult.DataEntity dataEntity : resultList) {
                    list.add(dataEntity);
                }
                if (resultList.size() < BaseConstant.PAGER_COUNT) {
                    ultimateRecyclerView.endFinish(true);
                } else {
                    ultimateRecyclerView.endFinish(false);
                }
                if (list.size() == 0 && task != null) {
                    task.addEmptyView("", "", R.mipmap.base_view_link_error);
                }
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


      /*  adapter.setIOnItemClickListener(new IOnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //BroadcastUtil.sendRecommendFotonBroadcast1(RecommendFilterActivity1.this,recommendFilterEntity);
            }
        });
*/
    }
}